<template>
	<view class="order-detail">
		<view v-if="loading" class="loading">
			<text>加载中...</text>
		</view>
		<view v-else-if="error" class="error">
			<text>{{ error }}</text>
			<button @click="fetchOrderDetail">重新加载</button>
		</view>
		<view v-else class="order-content">
			<view class="order-header">
				<text class="order-title">订单详情</text>
				<text class="order-status">{{ getOrderStatusText(order.status) }}</text>
				<text class="order-id">订单号: {{ order.orderId }}</text>
				<text class="order-id">下单时间: {{ order.createTime }}</text>
			</view>
			
			<view class="order-items">
				<view class="order-items-title">商品信息</view>
				<view class="item" v-for="item in order.items" :key="item.productId">
					<image :src="item.image" :alt="item.name"></image>
					<view class="item-info">
						<text class="item-name">{{ item.name }}</text>
						<text class="item-price">¥{{ item.price }}</text>
						<text class="item-quantity">数量: {{ item.quantity }}</text>
					</view>
				</view>
			</view>
			
			<view class="order-summary">
				<view class="order-summary-title">费用明细</view>
				<view class="summary-row">
					<text>商品总额</text>
					<text>¥{{order.totalPrice}}</text>
				</view>
				<view class="summary-row">
					<text>运费</text>
					<text>¥0.00</text>
				</view>
				<view class="summary-row total">
					<text>实付款</text>
					<text class="total-price">¥{{ order.totalPrice }}</text>
				</view>
			</view>
			
			<view class="order-actions">
				<text class="status-text">订单状态: {{ getOrderStatusText(order.status) }}</text>
				<button 
					v-if="order.status === 3" 
					class="action-btn" 
					@click="confirmReceive"
				>
					确认收货
				</button>
				<button 
					v-if="order.status === 4" 
					class="action-btn refund-btn" 
					@click="applyRefund"
				>
					申请退货
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				order: {
					orderId: '',
					status: 1,
					totalPrice: 0,
					items: []
				},
				loading: true,
				error: false
			}
		},
		onLoad(options) {
			const orderId = options.orderId;
			this.fetchOrderDetail(orderId);
		},
		methods: {
			getOrderStatusText(status) {
				const statusMap = {
					1: '待付款',
					2: '已付款待发货',
					3: '已到货，待收货',
					4: '已收货',
					5: '该订单已取消',
					6: '已申请退货，待审核',
					7: '已退货成功'
				};
				return statusMap[status] || '未知状态';
			},
			fetchOrderDetail(orderId) {
				this.loading = true;
				this.error = false;
				// 调用订单详情接口
				uni.request({
					url: `http://localhost:8080/api/orders/${orderId}`,
					method: 'GET',
					success: (res) => {
						if (res.data.code === 200) {
							this.order = res.data.data;
						} else {
							this.error = true;
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						this.error = true;
						uni.showToast({
							title: '获取订单详情失败',
							icon: 'none'
						});
					},
					complete: () => {
						this.loading = false;
					}
				});
			},
			confirmReceive() {
				uni.showModal({
					title: '确认收货',
					content: '确认收货后无法撤销，是否确认收货？',
					success: (res) => {
						if (res.confirm) {
							// 调用确认收货接口
							uni.request({
								url: 'http://localhost:8080/api/orders/receive',
								method: 'PUT',
								 data: {
									orderId: this.order.orderId
								},
								success: (res) => {
									if (res.data.code === 200) {
										uni.showToast({
											title: '确认收货成功',
											icon: 'success'
										});
										// 更新订单状态
										this.order.status = 4;
										// 通知订单列表页面刷新数据
										uni.$emit('orderStatusChanged', {
											orderId: this.order.orderId,
											status: 4
										});
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										});
									}
								},
								fail: (err) => {
									uni.showToast({
										title: '确认收货失败',
										icon: 'none'
									});
								}
							});
						}
					}
				});
			},
			applyRefund() {
				uni.showModal({
					title: '申请退货',
					content: '申请退货后需要等待审核，是否确认申请退货？',
					success: (res) => {
						if (res.confirm) {
							// 调用申请退货接口
							uni.request({
								url: 'http://localhost:8080/api/orders/refund',
								method: 'PUT',
								data: {
									orderId: this.order.orderId
								},
								success: (res) => {
									if (res.data.code === 200) {
										uni.showToast({
											title: '申请退货成功',
											icon: 'success'
										});
										// 更新订单状态
										this.order.status = 6;
										// 通知订单列表页面刷新数据
										uni.$emit('orderStatusChanged', {
											orderId: this.order.orderId,
											status: 6
										});
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										});
									}
								},
								fail: (err) => {
									uni.showToast({
										title: '申请退货失败',
										icon: 'none'
									});
								}
							});
						}
					}
				});
			},
			retry() {
				const orderId = this.order.orderId || this.$route.query.orderId;
				if (orderId) {
					this.fetchOrderDetail(orderId);
				}
			}
		}
	}
</script>

<style>
	.order-detail {
		padding: 15px;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.loading, .error {
		text-align: center;
		padding: 50px 0;
	}

	.loading text, .error text {
		font-size: 16px;
		color: #999;
	}

	.error button {
		margin-top: 20px;
		background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
		color: white;
		border: none;
		padding: 12px 25px;
		border-radius: 8px;
		font-size: 16px;
		box-shadow: 0 4px 10px rgba(64, 158, 255, 0.3);
	}

	.order-content {
		/* 保留原有样式 */
	}

	.order-header {
		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
		color: white;
		padding: 20px;
		border-radius: 15px;
		margin-bottom: 20px;
		box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
	}

	.order-status {
		font-size: 20px;
		font-weight: bold;
		margin-bottom: 8px;
		display: block;
	}

	.order-id {
		font-size: 15px;
		opacity: 0.9;
		margin-top: 8px;
		display: block;
	}

	.order-items {
		background-color: #fff;
		border-radius: 15px;
		padding: 20px;
		margin-bottom: 20px;
		box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
	}

	.order-items-title {
		font-size: 18px;
		font-weight: bold;
		color: #333;
		margin-bottom: 15px;
		padding-bottom: 10px;
		border-bottom: 2px solid #409eff;
	}

	.item {
		display: flex;
		margin-bottom: 20px;
		padding-bottom: 20px;
		border-bottom: 1px dashed #eee;
	}

	.item:last-child {
		margin-bottom: 0;
		padding-bottom: 0;
		border-bottom: none;
	}

	.item image {
		width: 90px;
		height: 90px;
		border-radius: 10px;
		margin-right: 15px;
		object-fit: cover;
		box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
	}

	.item-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.item-name {
		font-size: 17px;
		font-weight: bold;
		display: block;
		margin-bottom: 8px;
		color: #333;
	}

	.item-price {
		color: #ff4d4f;
		font-size: 17px;
		font-weight: bold;
		display: block;
		margin-bottom: 8px;
	}

	.item-quantity {
		font-size: 15px;
		color: #999;
	}

	.order-summary {
		background-color: #fff;
		border-radius: 15px;
		padding: 20px;
		margin-bottom: 20px;
		box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
	}

	.order-summary-title {
		font-size: 18px;
		font-weight: bold;
		color: #333;
		margin-bottom: 15px;
		padding-bottom: 10px;
		border-bottom: 2px solid #409eff;
	}

	.summary-row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 12px;
		font-size: 15px;
	}

	.summary-row:last-child {
		margin-bottom: 0;
	}

	.summary-row.total {
		border-top: 1px solid #eee;
		padding-top: 15px;
		margin-top: 15px;
		font-size: 18px;
		font-weight: bold;
	}

	.total-price {
		font-size: 20px;
		font-weight: bold;
		color: #ff4d4f;
	}

	.order-actions {
		background-color: #fff;
		border-radius: 15px;
		padding: 25px;
		text-align: center;
		box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
	}

	.status-text {
		font-size: 17px;
		color: #333;
		display: block;
		margin-bottom: 20px;
		font-weight: 500;
	}

	.action-btn {
		background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
		color: white;
		border: none;
		padding: 12px 25px;
		border-radius: 8px;
		font-size: 16px;
		font-weight: 500;
		box-shadow: 0 4px 10px rgba(64, 158, 255, 0.3);
		margin: 0 10px 15px 10px;
		min-width: 120px;
		transition: all 0.3s ease;
	}

	.action-btn:hover {
		transform: translateY(-2px);
		box-shadow: 0 6px 15px rgba(64, 158, 255, 0.4);
	}

	.action-btn:active {
		transform: translateY(0);
	}

	.refund-btn {
		background: linear-gradient(135deg, #ff7875 0%, #ff4d4f 100%);
		box-shadow: 0 4px 10px rgba(255, 77, 79, 0.3);
	}

	.refund-btn:hover {
		box-shadow: 0 6px 15px rgba(255, 77, 79, 0.4);
	}
</style>